package com.jbl.te.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jbl.commonutils.R;
import com.jbl.exceptionhandler.ManagerException;
import com.jbl.st.entity.CourseclassStudent;
import com.jbl.st.service.CourseclassStudentService;
import com.jbl.te.entity.*;
import com.jbl.te.entity.Courseclass;
import com.jbl.te.entity.dto.CourseclassData;
import com.jbl.te.entity.dto.CourseclassQuery;
import com.jbl.te.entity.vo.CourseclassVO;
import com.jbl.te.service.CourseService;
import com.jbl.te.service.CourseclassService;
import com.jbl.te.service.CourseclassTimeService;
import com.jbl.te.service.TeacherService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jbl
 * @since 2020-12-11
 */
@RestController
@RequestMapping("/te/courseclass")
@CrossOrigin
public class CourseclassController {

    @Autowired
    private CourseclassService courseclassService;
    @Autowired
    private CourseclassStudentService courseclassStudentService;

    @Autowired
    private CourseclassTimeService timeService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private CourseService courseService;



    @PostMapping("{page}/{limit}")
    @ApiOperation("分页课程班列表")
    public R pageList(@PathVariable("page") Long page, @PathVariable("limit") Long limit, @RequestBody(required = false) CourseclassQuery courseclassQuery) {
        Page<Courseclass> pageParam = new Page<>(page, limit);
        courseclassService.pageQuery(pageParam,courseclassQuery);
        List<Courseclass> records = pageParam.getRecords();

        List<CourseclassVO> list = new ArrayList<>();
        for (Courseclass courseclass : records) {
            CourseclassVO vo = new CourseclassVO();
            BeanUtils.copyProperties(courseclass,vo);

            Course course = courseService.getById(courseclass.getCourseId());
            if(course == null) throw new ManagerException(20001,"该课程班未匹配课程");
            BeanUtils.copyProperties(course,vo);

            Teacher teacher = teacherService.getById(courseclass.getTeacherId());
            if(teacher == null) throw new ManagerException(20001,"该课程班未匹配教师");
            vo.setTeacherId(teacher.getId());
            vo.setTeacherName(teacher.getName());
            //已选人数，未选人数
            QueryWrapper<CourseclassStudent> courseclassStudentQueryWrapper = new QueryWrapper<>();
            courseclassStudentQueryWrapper.eq("courseclass_id",courseclass.getId());
            courseclassStudentQueryWrapper.eq("state",1);
            vo.setSelected(courseclassStudentService.count(courseclassStudentQueryWrapper));
            vo.setNoneSelected(vo.getLimitNumber()-vo.getSelected());
            vo.setId(courseclass.getId());
            list.add(vo);
        }
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("list", list);
    }

    @GetMapping("/getCourseclassById")
    public R getCourseclassById(String id) {
        CourseclassData data = new CourseclassData();

        Courseclass courseclass = courseclassService.getById(id);
        if(courseclass == null) return R.error().message("课程班不存在");
        BeanUtils.copyProperties(courseclass,data);

        CourseclassTime time = timeService.getById(courseclass.getId());
        if(time == null) return R.error().message("课程班时间不存在");
        BeanUtils.copyProperties(time,data);

        Course course = courseService.getById(courseclass.getCourseId());
        if(course == null) return R.error().message("课程不存在");
        BeanUtils.copyProperties(course,data);

        Teacher teacher = teacherService.getById(courseclass.getTeacherId());
        if(teacher == null) return R.error().message("教师不存在");
        data.setTeacherId(teacher.getId());

        data.setId(courseclass.getId());

        return R.ok().data("courseclass",data);
    }

    //修改课程班
    @PutMapping("/updateCourseclass")
    public R updateCourseclass(@RequestBody CourseclassData courseclassData) {
        Boolean b = courseclassService.saveOrUpdateCourseclass(courseclassData);
        if(b) return R.ok();
        return R.error().message("修改失败");

    }

    //添加课程班
    @PostMapping("/saveCourseclass")
    public R saveCourseclass(@RequestBody CourseclassData courseclassData) {
        Boolean b = courseclassService.saveOrUpdateCourseclass(courseclassData);
        if(b) return R.ok();
        return R.error().message("添加失败");
    }

    //删除课程班
    @DeleteMapping("/delCourseclassById")
    public R delCourseclass(String id) {
        Courseclass courseclass = courseclassService.getById(id);
        if(courseclass.getIsConfirm())
            return R.error().message("无法删除状态为 [已确定] 课程班！");
        boolean remove = courseclassService.removeById(id);
        if(remove) {
            timeService.removeById(id);
        }
        if(remove) return R.ok();
        return R.error().message("删除失败");
    }

    //批量删除
    @DeleteMapping("/delCourseclassByIdList")
    @Transactional
    public R delCourseclassByIdList(@RequestBody List<String> idList) {
        for (String id : idList) {
            R r = delCourseclass(id);
            if(!r.getSuccess()) throw new ManagerException(20001,"中途有课程班删除失败");
        }
        return R.ok();
    }

}

